﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TuanAnhPersonalWeb.Models;
using TuanAnhPersonalWeb.Classes;
using MvcPaging;

namespace TuanAnhPersonalWeb.Areas.Admin.Controllers
{
    [Authorize(Roles = "admin,images")]
    public class ImageController : Controller
    {
        private TuanAnhEntities db = new TuanAnhEntities();
        private ImageAccessConsolidator access = new ImageAccessConsolidator();
        private const int defaultPageSize = 5;
        //
        // GET: /Admin/Image/
                  
        public ActionResult Index(int? page)
        {
            ViewData["Album"] = new SelectList(access.getAllAlbum(), "ALBUM_ID", "ALBUM_TITLE");
            int i = db.TUANANH__IMAGE.Count();
            ViewData["count"] = i;
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            IList<ImageModel> list = access.getAllImage().Where(e => e.ALBUM_ID == 1).ToList();
            if (Request.IsAjaxRequest())
            {
                return PartialView("_ListControl", list.ToPagedList(currentPageIndex, defaultPageSize));
            }
            else
            {
                return View(list.ToPagedList(currentPageIndex, defaultPageSize));
            }
        }
        //Search
        public ActionResult Search(int? page, string keySearch = "code", string valSearch = "", int? ALBUM_ID = 0)
        {
            ViewData["keySearch"] = keySearch;
            ViewData["valSearch"] = valSearch;
            ViewData["AlbumId"] = ALBUM_ID;
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            var list = access.getAllImage();
            list = list.Where(e => e.IMAGE_DESCRIPTION.ToLower().Contains(valSearch.ToLower()) && e.ALBUM_ID == ALBUM_ID).ToList().ToPagedList(currentPageIndex, defaultPageSize);
            if (Request.IsAjaxRequest())
            {
                return PartialView("_ListControl", list);
            }
            else
            {
                return View("Index", list);
            }

        }
        //
        // GET: /Admin/Image/Details/5

        public ViewResult Details(int id)
        {
            TUANANH__IMAGE tuananh__image = db.TUANANH__IMAGE.Single(t => t.IMAGE_ID == id);
            return View(tuananh__image);
        }

        //
        // GET: /Admin/Image/Create

       
        public ActionResult Create()
        {
            ViewData["Album"] = new SelectList(access.getAllAlbum(), "ALBUM_ID", "ALBUM_TITLE");
            return View();
        }

        //
        // POST: /Admin/Gallery/Create
       
        [HttpPost]
        public ActionResult Create(ImageModel model)
        {
            if (ModelState.IsValid)
            {
                access.insertImage(model);
                return RedirectToAction("Index");
            }
            ViewData["Album"] = new SelectList(access.getAllAlbum(), "ALBUM_ID", "ALBUM_TITLE");
            return View(model);
        }
        
        //
        // GET: /Admin/Image/Edit/5

       
        public ActionResult Edit(int id)
        {
            ImageModel model = access.getImagebyID(id);
            ViewData["Album"] = new SelectList(access.getAllAlbum(), "ALBUM_ID", "ALBUM_TITLE", model.ALBUM_ID);
            return View(model);
        }

        //
        // POST: /Admin/Gallery/Edit/5

        [HttpPost]
       
        public ActionResult Edit(ImageModel model)
        {
            if (ModelState.IsValid)
            {
                access.editImage(model);
                return RedirectToAction("Index");
            }
            ViewData["Album"] = new SelectList(access.getAllAlbum(), "ALBUM_ID", "ALBUM_TITLE", model.ALBUM_ID);
            return View(model);
        }

        //
        // GET: /Admin/Image/Delete/5
       
        public ActionResult Delete(int id)
        {
            TUANANH__IMAGE tuananh__image = db.TUANANH__IMAGE.Single(t => t.IMAGE_ID == id);
            return View(tuananh__image);
        }

        //
        // POST: /Admin/Image/Delete/5
        
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            TUANANH__IMAGE tuananh__image = db.TUANANH__IMAGE.Single(t => t.IMAGE_ID == id);
            db.TUANANH__IMAGE.DeleteObject(tuananh__image);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}